package com.ymatou.iapi.m.couponservice.testcase;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.GetSellerBatchesForAppBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerReceiveBatchInfo;
import com.ymatou.iapi.m.couponservice.parameter.SellerReceiveBatches;
import com.ymatou.iapi.m.couponservice.service.GetSellerBatchesForAppCall;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
/**
*获取商家批次列表信息(买手APP)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSellerBatchesForApp{
	private static GetSellerBatchesForAppBean getsellerbatchesforappBean;
	private static GetSellerBatchesForAppCall getsellerbatchesforappCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商家批次列表信息(买手APP)");
	}

	@Before
	public void caseUp() {
		getsellerbatchesforappBean=new GetSellerBatchesForAppBean();
		getsellerbatchesforappCall=new GetSellerBatchesForAppCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsellerbatchesforappCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerBatchesForApp_001() {
		Logger.start(true,"获取商家批次列表信息(买手APP)");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
	
			getsellerbatchesforappBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerbatchesforappBean.setBatchStatus(-1);
			getsellerbatchesforappBean.setPageSize(10);
			
			getsellerbatchesforappCall.setData(getsellerbatchesforappBean);
			getsellerbatchesforappCall.callService();	
			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerBatchesForApp_002() {
		Logger.start(true,"获取商家批次列表信息(买手APP)-LastBatchCreateTime不在范围呢");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			Date dNow = new Date(); // 当前时间
			Date dBefore = new Date();

			Calendar calendar1 = Calendar.getInstance(); // 得到日历
			calendar1.setTime(dNow);// 把当前时间赋给日历
			calendar1.add(Calendar.DAY_OF_MONTH, -10); // 设置为前一天
			dBefore = calendar1.getTime(); // 得到前一天的时间
			
			getsellerbatchesforappBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerbatchesforappBean.setBatchStatus(-1);
			getsellerbatchesforappBean.setPageSize(10);
			getsellerbatchesforappBean.setLastBatchCreateTime(dBefore);
			
			getsellerbatchesforappCall.setData(getsellerbatchesforappBean);
			getsellerbatchesforappCall.callService();	
			
			Logger.verifyEquals("0", getsellerbatchesforappCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getsellerbatchesforappCall.getMessage(), "验证返回Message");

			List<Map> result=new YmtCouponBatchWapper().getSellerBatchesForApp(getsellerbatchesforappBean.getPageSize(),sellercreatebatchBean.getSellerId(),getsellerbatchesforappBean.getBatchStatus(),getsellerbatchesforappBean.getLastBatchCreateTime());
			
			SellerReceiveBatches infos = getsellerbatchesforappCall.getBatchInfos();
			Logger.verifyEquals(0, infos.getBatchInfos().size(), "验证无返回conversionBillId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerBatchesForApp_003() {
		Logger.start(true,"获取商家批次列表信息(买手APP)-0:领取中 ");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);

			getsellerbatchesforappBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerbatchesforappBean.setBatchStatus(0);
			getsellerbatchesforappBean.setPageSize(10);
			
			getsellerbatchesforappCall.setData(getsellerbatchesforappBean);
			getsellerbatchesforappCall.callService();	
			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerBatchesForApp_004() {
		Logger.start(true,"获取商家批次列表信息(买手APP)- 1：停止发放 ");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);

			getsellerbatchesforappBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerbatchesforappBean.setBatchStatus(1);
			getsellerbatchesforappBean.setPageSize(10);
			
			getsellerbatchesforappCall.setData(getsellerbatchesforappBean);
			getsellerbatchesforappCall.callService();	
			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerBatchesForApp_005() {
		Logger.start(true,"获取商家批次列表信息(买手APP)- 2：过期");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);

			getsellerbatchesforappBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerbatchesforappBean.setBatchStatus(2);
			getsellerbatchesforappBean.setPageSize(10);
			
			getsellerbatchesforappCall.setData(getsellerbatchesforappBean);
			getsellerbatchesforappCall.callService();	
			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerBatchesForApp_006() {
		Logger.start(true,"获取商家批次列表信息(买手APP)- 3：已领完");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);

			getsellerbatchesforappBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerbatchesforappBean.setBatchStatus(3);
			getsellerbatchesforappBean.setPageSize(10);
			
			getsellerbatchesforappCall.setData(getsellerbatchesforappBean);
			getsellerbatchesforappCall.callService();	
			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn()
	{
		Logger.verifyEquals("0", getsellerbatchesforappCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", getsellerbatchesforappCall.getMessage(), "验证返回Message");

		List<Map> result=new YmtCouponBatchWapper().getSellerBatchesForApp(getsellerbatchesforappBean.getPageSize(),getsellerbatchesforappBean.getSellerId(),getsellerbatchesforappBean.getBatchStatus(),getsellerbatchesforappBean.getLastBatchCreateTime());
		
		SellerReceiveBatches infos = getsellerbatchesforappCall.getBatchInfos();
		Logger.verifyEquals(result.size(), infos.getBatchInfos().size(), "验证无返回batchinfo数量");

		List<SellerReceiveBatchInfo> sellerReceiveBatches = infos.getBatchInfos();
		for (int i=0;i<sellerReceiveBatches.size();i++)
		{
			SellerReceiveBatchInfo batch=sellerReceiveBatches.get(i);
			Logger.verifyEquals(Integer.parseInt(result.get(i).get("BatchId").toString()), batch.getBatchId(), "验证返回BatchId");
			Logger.verifyEquals(result.get(i).get("BatchCode").toString(), batch.getBatchCode(), "验证返回BatchCode");
			Logger.verifyEquals(result.get(i).get("BatchName").toString(), batch.getBatchName(), "验证返回BatchName");
			  
			Logger.verifyEquals(result.get(i).get("EffectiveStartTime").toString().subSequence(0, 19),
					batch.getEffectiveStartTime().replace("T", " "),
	                     "验证返回getEffectiveStartTime与数据库中表Ymt_CouponSetting中dValidStart字段相同");

	        Logger.verifyEquals(result.get(i).get("EffectiveEndTime").toString().subSequence(0, 19),
	        		batch.getEffectiveEndTime().replace("T", " "),
	                     "验证返回getEffectiveEndTime与数据库中表Ymt_CouponSetting中dValidEnd字段相同");
	        Logger.verifyEquals(0,
	        		batch.getOrderMinAmount().compareTo(new BigDecimal(result.get(i).get("OrderMinAmount").toString())),
                      "验证返回BatchCode与数据库中表中fMinOrderValue字段相同");
            Logger.verifyEquals(0,batch.getCouponAmount().compareTo(new BigDecimal(result.get(i).get("CouponAmount").toString())),
                "验证返回BatchCode与数据库中表中fMinOrderValue字段相同");   
            
            Logger.verifyEquals(result.get(i).get("dAddTime").toString().subSequence(0, 19),
	        		batch.getCreateTime().replace("T", " "),
	                     "验证返回getEffectiveEndTime与数据库中表Ymt_CouponSetting中dValidEnd字段相同");
            
            Logger.verifyEquals(Byte.parseByte(result.get(i).get("CouponType").toString()),
            		batch.getCouponType(),
                     "验证返回BatchCode与数据库中表Ymt_CouponBatch中coupontype字段相同");
	
		}
	}
}